package edu.whu.assignment8.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.whu.assignment8.dataobject.Product;
import edu.whu.assignment8.dataobject.ReturnObj;
import edu.whu.assignment8.dataobject.dto.ProductDto;
import edu.whu.assignment8.enums.ReturnEnums;
import edu.whu.assignment8.service.ProductService;
import edu.whu.assignment8.util.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/{pid}")
    @PreAuthorize("hasAuthority('user/query')")
    public ReturnObj findById(@PathVariable int pid) {
        Product product = productService.findProductById(pid);
        if(product == null) {
            return ReturnUtil.error(ReturnEnums.DataNotFound);
        }
        else {
            return ReturnUtil.success(product);
        }
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('user/query')")
    public ReturnObj findAll() {
        List<Product> products = productService.findAll();
        if(products.size() <= 0) {
            return ReturnUtil.error(ReturnEnums.DataNotFound);
        }
        else {
            return ReturnUtil.success(products);
        }
    }

    @PostMapping("")
    @PreAuthorize("hasAuthority('user/update')")
    public ReturnObj addProduct(@RequestBody Product product) {
        if(product == null) {
            return ReturnUtil.error(ReturnEnums.ParamError);
        }
        int result = productService.addProduct(product);
        if(result == 1) {
            return ReturnUtil.success();
        }
        else {
            return ReturnUtil.error(ReturnEnums.DataSaveError);
        }
    }

    @DeleteMapping("/{pid}")
    @PreAuthorize("hasAuthority('user/update')")
    public ReturnObj deleteProduct(@PathVariable int pid) {
        if(pid <= 0) {
            return ReturnUtil.error(ReturnEnums.ParamError);
        }
        int result = productService.deleteProduct(pid);
        if(result == 1) {
            return ReturnUtil.success();
        }
        else {
            return ReturnUtil.error(ReturnEnums.DataNotFound);
        }
    }

    @PutMapping("/{pid}")
    @PreAuthorize("hasAuthority('user/update')")
    public ReturnObj updateProduct(@PathVariable int pid, @RequestBody Product product) {
        if(pid <= 0) {
            return ReturnUtil.error(ReturnEnums.ParamError);
        }
        product.setPid(pid);
        int result = productService.updateProduct(product);
        if(result == 1) {
            return ReturnUtil.success();
        }
        else {
            return ReturnUtil.error(ReturnEnums.DataNotFound);
        }
    }

    @PostMapping("/{current}/{size}")
    @PreAuthorize("hasAuthority('user/query')")
    public ReturnObj selectPageAndCondition(@PathVariable Integer current,
                                            @PathVariable Integer size,
                                            @RequestBody Product condition) {
        if(current == null || size == null || condition == null) {
            return ReturnUtil.error(ReturnEnums.ParamError);
        }
        IPage<Product> result = productService.findPageAndCondition(current, size, condition);
        return ReturnUtil.success(result.getRecords());
    }

    @GetMapping("/findPSById/{pid}")
    @PreAuthorize("hasAuthority('user/query')")
    public ReturnObj findPSById(@PathVariable Integer pid) {
        if(pid == null ||pid <= 0) {
            return ReturnUtil.error(ReturnEnums.ParamError);
        }
        ProductDto result = productService.findPSById(pid);
        if(result == null) {
            return ReturnUtil.error(ReturnEnums.DataNotFound);
        }
        else {
            return ReturnUtil.success(result);
        }
    }
}
